<?php
/*
  网易云短信发送接口
  官方接口文档：http://dev.netease.im/docs/product/%E7%9F%AD%E4%BF%A1/%E7%9F%AD%E4%BF%A1%E6%8E%A5%E5%8F%A3%E6%8C%87%E5%8D%97
  
  @andy

*/  
class Netease_sms {
    private $AppKey = '';
    private $AppSecret = '';
    private $Nonce = '';
    private $CurTime = '';
    private $CheckSum = '';
    
    private $API_urls = array(
        "send" => "https://api.netease.im/sms/sendcode.action",
        "verify" => "https://api.netease.im/sms/verifycode.action",
        "send_template" => "https://api.netease.im/sms/sendtemplate.action",
        "query_status" => "https://api.netease.im/sms/querystatus.action"
    );


    public function __construct($app_key,$app_secret)
    {
        $this->AppKey = $app_key;
        $this->AppSecret = $app_secret;
    }
 
    private function nonce($len = 10){
       $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; 
       $password =''; 
       for ( $i = 0; $i < $len; $i++ ) { 
           $password .= $chars[ mt_rand(0, strlen($chars) - 1) ]; 
       } 
       return $password;  
    }    

   private function checksum(){
        $this->CurTime = time();
        $this->Nonce = $this->nonce();
        return sha1($this->AppSecret . $this->Nonce . $this->CurTime);
    }

private function http_post($send_type,$post_data){

     $url = $this->API_urls[$send_type];
     $this->CheckSum = $this->checksum();
     $ch = curl_init ();
     curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
        'AppKey:'.$this->AppKey,
        'Nonce:'.$this->Nonce,
        'CurTime:'.$this->CurTime,
        'CheckSum:'.$this->CheckSum,
        'Content-Type: application/x-www-form-urlencoded'
     ));

     /* Disable peer’s certificate */
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt( $ch, CURLOPT_POST, count ( $post_data ) );
     curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
     curl_setopt( $ch, CURLOPT_URL, $url );
     curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query ( $post_data ) );
     ob_start();
     curl_exec( $ch );
     $result = ob_get_contents();
     ob_end_clean();
     curl_close( $ch );
     return $result;


}

/*
 发送验证码
 参数：
   mobile:接收手机号
   deviceId:设备ID，可为空
   templateid:短信模板编号
   codeLen:验证码长度
*/
   public function send_code($mobile='',$deviceId='',$templateid = '',$codeLen = ''){
  
    $post_data = array('mobile'=>$mobile,'deviceId'=>$deviceId,'templateid'=>$templateid,'codeLen'=>$codeLen);   
    $result = $this->http_post('send',$post_data); 
    return $result;
   }


/*
  验证验证码
*/
   public function verify_code($mobile='',$code=''){
      $post_data = array('mobile'=>$mobile,'code'=>$code);
      $result = $this->http_post('verify',$post_data);
      return $result;
  }


/*
 发送模板通知
*/

   public function send_template($templateid = '',$mobiles='',$params = ''){

    $post_data = array('mobiles'=>$mobiles,'templateid'=>$templateid,'params'=>$params);
    $result = $this->http_post('send_template',$post_data);
    return $result;
   }



}

?>
